<?php
namespace app\admin\controller;

use think\Db;
use think\Loader;
use think\Request;

class Managegroup extends Admin
{
    /*
     * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     *  用户组
     * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     * */
    public function GroupList()
    {
        // 并且每页显示10条数据
        $list = Db::name('AuthGroup')->paginate(20);
        $this->assign('list', $list);
        return $this->fetch('group_list');
    }

    /*
     *  添加或编辑用户组
     * */
    public function GroupAdd()
    {
        $id = input('id', '', 'intval');
        $AuthGroup_mod = Db::name('AuthGroup');
        if (Request::instance()->isPOST()) {
            $data['title'] = input('post.title', '', 'op_t');
            $data['status'] = input('post.status', '0', 'intval');
            $validate = Loader::validate('AuthGroup');
            $res = $validate->check($data);
            if (!$res) {
                $this->error($validate->getError());
            }
            if ($id) {
                $data['id'] = $id;
                $flag = $AuthGroup_mod->update($data);
            } else {
                $data['addtime'] = time();
                $flag = $AuthGroup_mod->insert($data);
            }
            if ($flag) {
                $this->success('操作成功', 'Managegroup/GroupList');
            } else {
                $this->error('操作失败');
            }
        }
        if ($id) {
            $row = $AuthGroup_mod->where('id', $id)->find();
            $this->assign('row', $row);
        }
        $this->assign('id', $id);
        return $this->fetch('group_edit');
    }

    /*
     *  设置用户组状态
     * */
    public function GroupSetStatus()
    {
        $id = input('post.id', '', 'intval');
        if (empty($id)) {
            $this->error('操作有误!');
        }
        $status = Db::name('AuthGroup')->where('id', $id)->value('status');
        $status = $status ? 0 : 1;
        $flag = Db::name('AuthGroup')->where('id', $id)->update(['status' => $status]);
        if ($flag) {
            $this->success('修改成功');
        } else {
            $this->error('修改失败');
        }
    }

    /*
     * 删除用户组
     * */
    public function GroupDel()
    {
        $id = input('post.id', '', 'intval');
        if (empty($id)) {
            $this->error('操作有误!');
        }
        if ($id == 1) {
            $this->error('超级管理员不能删除');
        }
        $flag = Db::name('AuthGroup')->delete($id);
        if ($flag) {
            $this->success('删除成功!');
        } else {
            $this->error('删除失败!');
        }
    }

    /*
     * 用户组授权
     * */
    public function GroupAccess()
    {
        $id = input('id', '', 'intval');
        $row = Db::name('AuthGroup')->where('id', $id)->find();
        if (!$row) {
            $this->error('操作有误');
        }
        if (Request::instance()->isPost()) {
            $new_rules = $_POST['new_rules'];
            if (!is_array($new_rules)) {
                $this->error('操作有误');
            }
            $data['rules'] = implode(',', $new_rules);
            $flag = Db::name('AuthGroup')->where('id', $id)->update($data);
            if ($flag) {
                $this->success('操作成功', url('Managegroup/GroupAccess', array('id' => $id)));
            } else {
                $this->error('操作失败');
            }
        }
        $list = Db::name('AuthRule')->field('id,name,title,css,pid')->where('status=1')->select();
        $data['list'] = list_to_tree($list);
        $row['rules'] = explode(',', $row['rules']);
        $data['row'] = $row;
        $this->assign($data);
        $this->assign('id', $id);
        return $this->fetch('group_access');
    }
}
